package enterprise.manage.util;

/**
 * Created by IntelliJ IDEA.
 * User: panxuanang
 * Date: 2010-12-14
 * Time: 11:29:17
 * Desc:
 */

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Vector;

public class ExcelReading {
    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelReading.class);

    public ExcelReading() {
    }

    public Vector<Hashtable> Read(FileItem fileItem) throws FileUploadException, IOException {
        Vector<Hashtable> vct = new Vector<Hashtable>();
//        try {
//            String regExp = ".+\\\\(.+)$";

        String correctType = ".xls";
//            Pattern p = Pattern.compile(regExp);

        FileItem item = fileItem;
        String name = item.getName();
//                LOGGER.info("fileName=" + name);
        long size = item.getSize();
        if ((name == null || name.equals("")) && size == 0) {
            return null;
        }
//                Matcher m = p.matcher(name);
//                boolean result = m.find();
        boolean result = false;
        if (name != null) {
            result = name.endsWith(correctType);
        }
        if (result) {
            try {

                InputStream is = item.getInputStream(); // 读取输入流，也就是上传的文件内容

                Workbook rwb = Workbook.getWorkbook(is);
                Sheet rs = rwb.getSheet(0);
                int rsColumns = rs.getColumns(); // 获得列数
                int rsRows = rs.getRows();// 获得行数
                Cell[] cell;
                java.util.Hashtable<String, String> hst;

                for (int i = 1; i < rsRows; i++) {
                    hst = new Hashtable<String, String>();
                    cell = rs.getRow(i);
                    for (int j = 0; j < cell.length; j++) {
                        if (j == 0) {
                            hst.put("mobile", cell[j].getContents());
                        } else if (j == 1) {
                            hst.put("payMoney", cell[j].getContents());
                        } else if (j == 2) {
                            hst.put("productName", cell[j].getContents());
                        } else if (j == 3) {
                            hst.put("partnerMark1", cell[j].getContents());
                        } else if (j == 4) {
                            hst.put("partnerMark2", cell[j].getContents());
                        }
//                                hst.put("fee_type", 0);
                    }
                    vct.add(hst);
                }

                rwb.close();
                is.close();
            }

            catch (Exception e) {
                LOGGER.error("error", e);
            }

        } else {
            throw new IOException("文件类型不对");
        }


//        }
//        catch (FileUploadBase.SizeLimitExceededException e) {
//            LOGGER.error("error", "超过最大读取长度");
//        }
//        catch (Exception e) {
//            e.printStackTrace();
//        }
        return vct;

    }
/*
	public Vector Check_Info(String agent_id, HttpServletRequest request)
			throws Exception {
		//System_Check.Agent agent = new System_Check.Agent();
		 //step_one.UserInput userinput = new step_one.UserInput();
		Vector xlsinfo = Read2(request);

		Hashtable info = null;
		Vector vct = new Vector();
		Vector vct_allpass = new Vector();
		Vector vct_allunpass = new Vector();

		if (xlsinfo == null)
			return null;
		int total_no = xlsinfo.size();// 总共提交的个数
		Hashtable<String, String> pass_info = null;// 可以缴费的个数
		Hashtable<String, String> unpass_info = null;// 不可以缴费的个数
		boolean cheap_province = false;
		if (total_no <= 0)
			return null;
		String agent_level = agent.Get_Agent_Level(agent_id);// 取得代理级别
		for (int i = 0; i < total_no; i++) {
			String mobile_num;
			String fee_money;
			String pay_money;
			String province = "";
			String city = "";
			String limittime = "";
			int flag = 0;
			pass_info = new Hashtable<String, String>();
			unpass_info = new Hashtable<String, String>();
			info = (Hashtable) xlsinfo.elementAt(i);
			mobile_num = (String) info.get("mobile_num");
			fee_money = (String) info.get("money");
			if (info.containsKey("limittime"))
				limittime = (String) info.get("limittime");
			else
				limittime = userinput.Get_Lasttime2(2);
			agent.Add_Mobile_Multi(agent_id, mobile_num, fee_money);
			Hashtable seg_info = userinput.Seg_Check(mobile_num);
			if (seg_info != null) {
				province = (String) seg_info.get("province");
				city = (String) seg_info.get("city");
				flag = ((Integer) seg_info.get("state")).intValue();
			}
			cheap_province = province
					.matches("(北京|上海|山东|广东|江苏|福建|四川|天津|辽宁|浙江)");
			if (flag == 1 && mobile_num.length() == 11) {// 表示可以为该号段的手机缴费
				if (userinput.Fee_TorF2(province)) {// 表示该省还没达到最大限额
					if ("50".equals(fee_money)) {
						if (cheap_province) {
							pay_money = userinput.Get_ProvincePrice(province,
									"50", agent_level);// 代理充值100的价格降价后
						}
						else {
							pay_money = agent.Get_Per_Money(agent_id, 50);// 代理充值100的价格
						}
						pass_info.put("mobile_num", mobile_num);
						pass_info.put("fee_money", fee_money);
						pass_info.put("pay_money", pay_money);
						pass_info.put("province", province);
						pass_info.put("city", city);
						pass_info.put("limittime", limittime);
					}
					else if ("100".equals(fee_money)) {
						if (cheap_province) {
							pay_money = userinput.Get_ProvincePrice(province,
									"100", agent_level);// 代理充值100的价格降价后
						}
						else {
							pay_money = agent.Get_Per_Money(agent_id, 100);// 代理充值100的价格
						}
						pass_info.put("mobile_num", mobile_num);
						pass_info.put("fee_money", fee_money);
						pass_info.put("pay_money", pay_money);
						pass_info.put("province", province);
						pass_info.put("city", city);
						pass_info.put("limittime", limittime);
					}
					else if (Integer.parseInt(fee_money) <= 1000
							&& Integer.parseInt(fee_money) % 100 == 0) {
						if (cheap_province) {
							pay_money = userinput.Get_ProvincePrice(province,
									"100", agent_level);// 代理充值100的价格降价后
						}
						else {
							pay_money = agent.Get_Per_Money(agent_id, 100);// 代理充值100的价格
						}
						pay_money = "" + Float.parseFloat(pay_money)
								* (Integer.parseInt(fee_money) / 100);
						pass_info.put("mobile_num", mobile_num);
						pass_info.put("fee_money", fee_money);
						pass_info.put("pay_money", pay_money);
						pass_info.put("province", province);
						pass_info.put("city", city);
						pass_info.put("limittime", limittime);
					}
					else {// 用户选择金额不正确
						unpass_info.put("mobile_num", mobile_num);
						unpass_info.put("fee_money", fee_money);
						unpass_info.put("descript", "缴费金额不正确");
					}
				}
				else {
					unpass_info.put("mobile_num", mobile_num);
					unpass_info.put("fee_money", fee_money);
					unpass_info.put("descript", "系统暂不支持对该手机号码的缴费");
				}
			}
			else {
				unpass_info.put("mobile_num", mobile_num);
				unpass_info.put("fee_money", fee_money);
				unpass_info.put("descript", "系统暂不支持对该手机号码的缴费");
			}
			if (pass_info.size() > 0) {
				vct_allpass.add(pass_info);
			}
			else if (unpass_info.size() > 0) {
				vct_allunpass.add(unpass_info);
			}
		}
		vct.add(total_no);// 总共提交的个数
		vct.add(vct_allpass); // 所有成功信息
		vct.add(vct_allunpass); // 所有失败信息
		return vct;
	}
	*/
}